package com.yswee.framework.widget.base;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import com.yswee.framework.utils.OSUtils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListView;

public class MListView extends ListView {

	protected Context mContext;
	
	public MListView(Context context) {
		super(context);
		initializeView(context);
	}
	
	public MListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initializeView(context);
	}
	
	public MListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initializeView(context);
	}
	
	protected void initializeView(Context context) {
		mContext = context;
	}
	
	public void scrollToTop() {
		if (!isStackFromBottom()) {
			setStackFromBottom(true);
		}
		setStackFromBottom(false);
	}
	
	public void scrollToBottom() {
		if(getAdapter() != null) {
			setSelection(getAdapter().getCount() - 1);
		}
	}
	
	public boolean isScrollAtTop() {
		if (getCount() == 0) {
			return true;
		} else if (getFirstVisiblePosition() == 0) {
			final View firstVisibleChild = getChildAt(0);
			if (firstVisibleChild != null) {
				return firstVisibleChild.getTop() >= getTop();
			}
		}
		return false;
	}

	public boolean isScrollAtBottom() {
		final int count = getCount();
		final int lastVisiblePosition = getLastVisiblePosition();
		if (count == 0) {
			return true;
		} else if (lastVisiblePosition == count - 1) {
			final int childIndex = lastVisiblePosition - getFirstVisiblePosition();
			final View lastVisibleChild = getChildAt(childIndex);

			if (lastVisibleChild != null) {
				return lastVisibleChild.getBottom() <= getBottom();
			}
		}
		return false;
	}
	
	public void setVerticalTrackDrawable(int resId) {
		setVerticalTrackDrawable(getResources().getDrawable(resId));
	}
	
	public void setVerticalTrackDrawable(Drawable d) {
		try {
			Field mScrollCacheField = View.class.getDeclaredField("mScrollCache");
		    mScrollCacheField.setAccessible(true);
		    Object mScrollCache = mScrollCacheField.get(this);

		    Field scrollBarField = mScrollCache.getClass().getDeclaredField("scrollBar");
		    scrollBarField.setAccessible(true);
		    Object scrollBar = scrollBarField.get(mScrollCache);

		    Method method = scrollBar.getClass().getDeclaredMethod("setVerticalTrackDrawable", Drawable.class);
		    method.setAccessible(true);
		    method.invoke(scrollBar, d);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void setVerticalThumbDrawable(int resId) {
		setVerticalThumbDrawable(getResources().getDrawable(resId));
	}
	
	public void setVerticalThumbDrawable(Drawable d) {
		try {
			Field mScrollCacheField = View.class.getDeclaredField("mScrollCache");
		    mScrollCacheField.setAccessible(true);
		    Object mScrollCache = mScrollCacheField.get(this);

		    Field scrollBarField = mScrollCache.getClass().getDeclaredField("scrollBar");
		    scrollBarField.setAccessible(true);
		    Object scrollBar = scrollBarField.get(mScrollCache);

		    Method method = scrollBar.getClass().getDeclaredMethod("setVerticalThumbDrawable", Drawable.class);
		    method.setAccessible(true);
		    method.invoke(scrollBar, d);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 隐藏上下阴影
	 */
	public void hideFadingEdge() {
		setFadingEdgeLength(0);
		if(OSUtils.hasGingerbread()) {
			setOverScrollMode(OVER_SCROLL_NEVER);
		}
	}
	
}
